package cz.cuni.mff.ufal.httpserver.test;

import java.io.IOException;
import java.io.PrintStream;
import java.util.Map.Entry;

import cz.cuni.mff.ufal.httpserver.HttpRequestHandler;
import cz.cuni.mff.ufal.httpserver.HttpServer;
import cz.cuni.mff.ufal.httpserver.protocol.HttpRequest;
import cz.cuni.mff.ufal.httpserver.protocol.HttpResponse;

public class Main {

  /**
   * Just for testing purposes.
   *
   * @param args
   * @throws InterruptedException
   * @throws IOException
   */
  public static void main(String[] args) throws InterruptedException, IOException {
    HttpServer server = new HttpServer(new HttpRequestHandler() {
      @Override
      public void process(HttpRequest request, HttpResponse response) {
        System.out.println("New incomming connection:");
        System.out.println(String.format("method: %s", request.getMethod()));
        System.out.println(String.format("path: %s", request.getRequestedUri().getPath()));
        for (Entry<String, String> param : request.getQueryParameters().entrySet())
          System.out.println(String.format("p %s = %s", param.getKey(), param.getValue()));
        for (Entry<String, String> header : request.getHeaders().entrySet())
          System.out.println(String.format("h %s : %s", header.getKey(), header.getValue()));
        response.writeHeaders();
        PrintStream cout = new PrintStream(response.getOutputStream());
        cout.println("hahaosiem?\nko, zdohc");
      }
    });
    server.setPort(16000);
    server.start();
    System.out.println("The server was successfully run, press ENTER to stop...");
    System.in.read();
    server.stop();
  }

}
